package com.lysj.order.order.feignImpl;

import cn.hutool.core.bean.BeanUtil;
import com.lysj.admin.common.food.domain.OrderItemVo;
import com.lysj.admin.common.food.domain.OrderVo;
import com.lysj.admin.common.order.domain.OrderItemAndFoodWeight;
import com.lysj.admin.common.order.domain.OrderModifyVo;
import com.lysj.admin.common.order.feign.OrderServiceFeign;
import com.lysj.admin.common.print.domain.PrintOrderVo;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.RandomUtil;
import com.lysj.order.order.cache.DeskNameApplicationRunner;
import com.lysj.order.order.domain.Order;
import com.lysj.order.order.domain.OrderItem;
import com.lysj.order.order.feign.DeskFeign;
import com.lysj.order.order.feign.PrinterFeign;
import com.lysj.order.order.service.OrderItemService;
import com.lysj.order.order.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 15:18 2019/4/1
 * @ Description: 下单接口
 **/

@RestController
public class OrderFeignImpl implements OrderServiceFeign {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderItemService orderItemService;

    @Resource
    private PrinterFeign printerFeign;

    @Resource
    private DeskFeign deskFeign;

    private final static Integer DESK_REPAST_STATUS = 3; //餐桌就餐状态

    /*
     * @author drj
     * @date 2019-04-01 16:57
     * @Description :下单接口
     */
    @Override
    @Transactional
    public Map<String, Object> createOrder(@RequestBody OrderVo orderVo) {
        Map<String, Object> map = new HashMap<>();

        //创建订单,初始化订单信息
        Order order = new Order();
        order.setOrderNumber(RandomUtil.createOrderCode(11));
        order.setDeskId(orderVo.getDeskId()); //餐桌id
        order.setCustomerId(orderVo.getCustomerId());//用户id
        order.setStoreId(orderVo.getStoreId()); //门店id
        order.setStatus(Order.Status.PLACEORDER.getCode()); //下单状态
        order.setCusAmount(orderVo.getCusAmount()); // 用餐人数
        orderService.save(order);
        //创建orderItem
        List<OrderItemAndFoodWeight> orderItemAndFoodWeightList = createOrderItem(order.getId(), orderVo.getOrderItemList());

        //计算总价
        BigDecimal sellMoney = orderItemService.getRepository().summaryMoney(order.getId());

        //初始化打印信息
        PrintOrderVo printOrderVo = new PrintOrderVo();
        printOrderVo.setStoreId(orderVo.getStoreId());
        printOrderVo.setOrderNumber(order.getOrderNumber());
        printOrderVo.setDeskName(DeskNameApplicationRunner.deskNameCache.get(orderVo.getDeskId(), false));

        //通过feign调用获改变餐桌状态为就餐状态
        deskFeign.changeDeskStatus(order.getDeskId(), DESK_REPAST_STATUS);

        printOrderVo.setTolMoney(sellMoney);
        //菜品详情
        printOrderVo.setOrderItemVoList(orderVo.getOrderItemList());

        //调用打单feign接口
        printerFeign.acceptOrderInfo(printOrderVo);
        map.put("orderItemAndFoodWeightList", orderItemAndFoodWeightList);
        map.put("orderId", order.getId());
        return map;
    }

    /*
     * @author drj
     * @date 2019-04-01 16:57
     * @Description ：加菜
     */
    @Override
    public List<OrderItemAndFoodWeight> orderModify(@RequestBody OrderModifyVo orderModifyVo) {
        //创建orderItem
        return createOrderItem(orderModifyVo.getOrderId(), orderModifyVo.getOrderItemList());
    }

    @Override
    public boolean orderIemModify(String orderItemId, BigDecimal tolPrice, String doway) {
        OrderItem orderItem = orderItemService.findOne(orderItemId);
        orderItem.setTolPrice(tolPrice);
        orderItem.setDoWay(doway);
        orderItemService.save(orderItem);
        return true;
    }

    //查询用户正在就餐的订单id
    @Override
    public String queryCusPlaceOrderId(String customerId) {
        Order order = orderService.getRepository().findByCustomerIdAndStatusAndDelFlag(customerId, Order.Status.PLACEORDER.getCode(), CommonConstant.NORMAL_FLAG);
        if (null == order) {
            return "";
        }
        return order.getId();
    }


    private List<OrderItemAndFoodWeight> createOrderItem(String orderId, List<OrderItemVo> orderItemList) {
        List<OrderItemAndFoodWeight> orderItemAndFoodWeights = new LinkedList<>();
        for (OrderItemVo orderItemVo : orderItemList) {
            //复制菜品信息
            OrderItem orderItem = new OrderItem();
            BeanUtil.copyProperties(orderItemVo, orderItem);
            orderItem.setOrderId(orderId);
            orderItemService.save(orderItem);
            //若该菜品需要称重
            if (!ParamUtil.isBlank(orderItemVo.getWeighFoodId())) {
                OrderItemAndFoodWeight orderItemAndFoodWeight = new OrderItemAndFoodWeight();
                orderItemAndFoodWeight.setOrderItemId(orderItem.getId());
                orderItemAndFoodWeight.setWeighFoodId(orderItemVo.getWeighFoodId());
                orderItemAndFoodWeights.add(orderItemAndFoodWeight);
            }
        }
        return orderItemAndFoodWeights;
    }

    //正在就餐的餐桌进行换桌
    @Override
    public boolean changeDesk(String sourceDeskId, String targetDeskId) {
        //查询正在开桌的订单
        Order order = orderService.getRepository().findByDeskIdAndStatusAndDelFlag(sourceDeskId, Order.Status.PLACEORDER.getCode(), CommonConstant.NORMAL_FLAG);
        if (null == order) {
            return false;
        }
        //修改绑定的餐桌id
        order.setDeskId(targetDeskId);
        orderService.save(order);
        return true;
    }
}
